<?php

namespace App\Http\Controllers;

use App\Http\Model\Hint;
use App\Http\Services\RoomProtectServices;
use App\Http\Services\UserServices;
use Illuminate\Http\Request;
use App\Lib\Response;


class RoomProtectController extends Controller
{
    protected $services_user;
    protected $roomProtectServices;

    public function __construct()
    {
        $this->services_user = new UserServices();
        $this->roomProtectServices = new RoomProtectServices();
    }

    //获取对应房间下的房间守护榜
    public function getRoomProtectRank(Request $request){
        $roomId = $request->get('room_id',0);
        $page = $request->get('page',0);
        $limit = $request->get('limit',10);
        if (empty($roomId)){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $user = $this->services_user->get_current_user();
        if(empty($user)){
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $userId = $user['user_id'];
        $type = $request->get('type',1);//日榜/总榜 1=日 2总
        $data = $this->roomProtectServices->getRoomProtectRank($roomId, $userId, $type,$page, $limit);
        if($data['code'] != Hint::SUCCESS){
            Response::returnResult(Hint::ERR_NORMAL,$data['data'],$data['msg']);
        }
        Response::returnResult(Hint::SUCCESS,$data['data'],$data['msg']);
    }

    //获取守护商品列表
    public function getRoomProtectList(Request $request){
        $roomId = $request->get('room_id',0);
        if (empty($roomId)){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $user = $this->services_user->get_current_user();
        if(empty($user)){
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $userId = $user['user_id'];
        $data = $this->roomProtectServices->getRoomProtectList($roomId, $userId);
        if($data['code'] != Hint::SUCCESS){
            Response::returnResult(Hint::ERR_NORMAL,$data['data'],$data['msg']);
        }
        Response::returnResult(Hint::SUCCESS,$data['data'],$data['msg']);
    }

    //支付商品服务
    public function payRoomProtect(Request $request){
        $user = $this->services_user->get_current_user();
        if(empty($user)){
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $userId = $user['user_id'];
        $pay_type = $request->post('pay_type',1);//支付类型 1=开通 2=续费 3=升级
        $service_id = $request->post('service_id',1);//支付服务id 1=黄金 2=白银 3=青铜
        $to_user_id = $request->post('to_user_id',0);//主持麦id
        $roomId = $request->post('room_id',0);//房间id
        $data = $this->roomProtectServices->payService($userId, $pay_type, $service_id, $to_user_id, $roomId);
        if($data['code'] != Hint::SUCCESS){
            Response::returnResult($data['code'],$data['data'],$data['msg']);
        }
        Response::returnResult(Hint::SUCCESS,$data['data'],$data['msg']);
    }

    //支付弹出窗
    public function payRoomProtectNotice(Request $request){
        $user = $this->services_user->get_current_user();
        if(empty($user)){
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $userId = $user['user_id'];
        $service_id = $request->post('service_id',1);//支付服务id 1=黄金 2=白银 3=青铜
        $roomId = $request->post('room_id',0);
        $data = $this->roomProtectServices->payServiceNotice($userId,  $service_id, $roomId);
        if($data['code'] != Hint::SUCCESS){
            Response::returnResult(Hint::ERR_NORMAL,$data['data'],$data['msg']);
        }
        Response::returnResult(Hint::SUCCESS,$data['data'],$data['msg']);
    }
}